package adventure.misc;

import adventure.room.RoomManager;
import colour.ColourUtil;

/**
 *
 * @author Michael Hanns
 *
 */
public class MapGenImp implements MapGen {

    private RoomManager rooms;
    private String map[][];

    public MapGenImp(RoomManager r) {
        this.rooms = r;
        this.map = new String[24][24];
    }

    @Override
    public String[][] getMap(int x, int y, int z) {
        for (int xCount = 0; xCount < 8; xCount++) {
            for (int yCount = 0; yCount < 8; yCount++) {
                map[xCount * 3][yCount * 3] = " ";
                map[xCount * 3 + 1][yCount * 3] = getSouth(x - 4 + xCount, y - 4 + yCount, z);
                map[xCount * 3 + 2][yCount * 3] = " ";
                map[xCount * 3][yCount * 3 + 1] = getWest(x - 4 + xCount, y - 4 + yCount, z);
                map[xCount * 3 + 1][yCount * 3 + 1] = getRoom(x - 4 + xCount, y - 4 + yCount, z, x, y);
                map[xCount * 3 + 2][yCount * 3 + 1] = getEast(x - 4 + xCount, y - 4 + yCount, z);
                map[xCount * 3][yCount * 3 + 2] = " ";
                map[xCount * 3 + 1][yCount * 3 + 2] = getNorth(x - 4 + xCount, y - 4 + yCount, z);
                map[xCount * 3 + 2][yCount * 3 + 2] = " ";
            }
        }
        return map;
    }

    private String getRoom(int x, int y, int z, int px, int py) {
        if (x < 100 && y < 100 && z < 100) {
            if (x == px && y == py) {
                return ColourUtil.colourise("X", ColourUtil.green);
            }
            if (rooms.roomExists(x, y, z)) {
                return ColourUtil.colourise("◻", ColourUtil.white);
            }
        }
        return " ";
    }

    private String getNorth(int x, int y, int z) {
        if (rooms.roomExists(x, y, z) && rooms.roomExists(x, y + 1, z)) {
            if (rooms.isBlocked(x, y, z, 'N')) {
                return " ";
            } else if (rooms.isLocked(x, y, z, 'N')) {
                return ColourUtil.colourise("-", ColourUtil.red);
            } else if (rooms.isClosed(x, y, z, 'N')) {
                return ColourUtil.colourise("+", ColourUtil.yellow);
            }
            return ColourUtil.colourise("|", ColourUtil.grey);
        }
        return " ";
    }

    private String getEast(int x, int y, int z) {
        if (rooms.roomExists(x, y, z) && rooms.roomExists(x + 1, y, z)) {
            if (rooms.isBlocked(x, y, z, 'E')) {
                return " ";
            } else if (rooms.isLocked(x, y, z, 'E')) {
                return ColourUtil.colourise("|", ColourUtil.red);
            } else if (rooms.isClosed(x, y, z, 'E')) {
                return ColourUtil.colourise("+", ColourUtil.yellow);
            }
            return ColourUtil.colourise("-", ColourUtil.grey);
        }
        return " ";
    }

    private String getSouth(int x, int y, int z) {
        if (rooms.roomExists(x, y, z) && rooms.roomExists(x, y - 1, z)) {
            if (rooms.isBlocked(x, y, z, 'S')) {
                return " ";
            } else if (rooms.isLocked(x, y, z, 'S')) {
                return ColourUtil.colourise("-", ColourUtil.red);
            } else if (rooms.isClosed(x, y, z, 'S')) {
                return ColourUtil.colourise("+", ColourUtil.yellow);
            }
            return ColourUtil.colourise("|", ColourUtil.grey);
        }
        return " ";
    }

    private String getWest(int x, int y, int z) {
        if (rooms.roomExists(x, y, z) && rooms.roomExists(x - 1, y, z)) {
            if (rooms.isBlocked(x, y, z, 'W')) {
                return " ";
            } else if (rooms.isLocked(x, y, z, 'W')) {
                return ColourUtil.colourise("|", ColourUtil.red);
            } else if (rooms.isClosed(x, y, z, 'W')) {
                return ColourUtil.colourise("+", ColourUtil.yellow);
            }
            return ColourUtil.colourise("-", ColourUtil.grey);
        }
        return " ";
    }
}
